//
//  DIMIcreateController.m
//  迷笛
//
//  Created by . on 2018/9/13.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDIcreateController.h"
#import "MIDIcreateCell.h"
#import "MIDImycreatemodel.h"
@interface MIDIcreateController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * createTable;
@property (nonatomic,retain) NSArray *viewControllers;
@property(nonatomic,copy)NSString *Mycreateapi;
@property(nonatomic,copy)NSString * apptoken;

@property(nonatomic,strong)NSMutableArray * createArr;
@end

@implementation MIDIcreateController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.apptoken = [user objectForKey:@"APPToken"];
//    [self MycreateRequest];
//    self.createArr = [NSMutableArray array];

    self.view.backgroundColor =[UIColor colorWithHexString:@"#171717"];
    [self HomeUI];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self MycreateRequest];
     self.createArr = [NSMutableArray array];
}
//- (void)viewWillAppear:(BOOL)animated
// {
//    [super viewWillAppear:YES];
//     [self MycreateRequest];
//    self.createArr = [NSMutableArray array];
//
// }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.createArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDIcreateCell * cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[MIDIcreateCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
        [_createTable setSeparatorColor : [UIColor blackColor]];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    MIDImycreatemodel * mm1 = self.createArr[indexPath.row];
    
//    cell.heardimgea.image = [UIImage imageNamed:@"IMG_0195.jpg"];
//    cell.name.text =@"王者王者";
//    cell.fous.text = @"关注366";
//    cell.post.text = @"帖子数255";

    [cell.heardimgea sd_setImageWithURL:[NSURL URLWithString:mm1.cover] placeholderImage:nil];
    cell.name.text =mm1.name;
    cell.fous.text = [NSString stringWithFormat:@"关注%@",mm1.join];
    cell.post.text = [NSString stringWithFormat:@"帖子数%@",mm1.post];

    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDevice_Is_iPhoneX?HeighX(70):HeightScale(70);
}
- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
    return nil;
}
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
    return nil;
}


-(void)HomeUI
{
    [self.view addSubview:self.createTable];
}
-(UITableView *)createTable
{
    if (!_createTable)
    {
        _createTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 70) style:UITableViewStyleGrouped];
        _createTable.dataSource = self;
        _createTable.delegate = self;
        _createTable.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
    }
    return _createTable;
}
-(void)MycreateRequest
{
    NSString * circle = @"users/create/circle";
    self.Mycreateapi = [NSString stringWithFormat:@"%@%@?token=%@",MIDIApiRoot,circle,self.apptoken];
    NSLog(@"我创建的地址%@",self.Mycreateapi);
    [EdHttp get:self.Mycreateapi params:nil progress:^(id downloadProgress)
     {
         
     }
        success:^(id responseObject)
     {
         NSLog(@"我创建的圈子%@",responseObject);
         NSArray *arr4 = responseObject[@"data"];
         for (NSDictionary *dic4 in arr4)
         {
             NSString * circlename;
             NSString * circleiamge;
             NSString * join_peopel;
             NSString * post_people;
             NSNumber * is_join;
             if (![dic4[@"name"] isKindOfClass:[NSNull class]]){
                 circlename = dic4[@"name"];
             }
             else{
                 circlename = @"";
             }
             if (![dic4[@"cover"] isKindOfClass:[NSNull class]]){
                 circleiamge = dic4[@"cover"];
             }
             else{
                 circleiamge = @"";
             }
             if (![dic4[@"join_peopel"] isKindOfClass:[NSNull class]]){
                 join_peopel = dic4[@"join_peopel"];
             }
             else{
                 join_peopel = @"";
             }
             if (![dic4[@"post_people"] isKindOfClass:[NSNull class]]){
                 post_people = dic4[@"post_people"];
             }
             else{
                 post_people = @"";
             }
             if (![dic4[@"is_join"] isKindOfClass:[NSNull class]]){
                 is_join = dic4[@"is_join"];
             }
             else{
                 //                 is_join = @"";
             }
             
             MIDImycreatemodel * mmact = [MIDImycreatemodel new];
             mmact.modelId = dic4[@"id"];
             mmact.name = circlename;
             mmact.cover =circleiamge;
             mmact.join = join_peopel;
             mmact.post = post_people;
             mmact.isjoin = is_join;

             [self.createArr addObject:mmact];
             
         }
         dispatch_async(dispatch_get_main_queue(), ^{
             [self.createTable reloadData];
             
         });
     }
        failure:^(NSError *error)
     {
         NSLog(@"失败");
     }];
}
@end
